/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.model.domain.Model;
import org.ediba.siri.core.model.domain.ModelAddress;
import org.ediba.siri.core.model.service.ModelService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class ModelAddressController extends Controller {

	private ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ModelAddressCommand command){

		Integer idModel = null;
		String street = null;
		Integer number = null;
		
		idModel = command.getIdModel();
		try {street = command.getData().getString("street");
		} catch (JSONException e) {e.printStackTrace();}
		try {number = command.getData().getInt("number");
		} catch (JSONException e) {e.printStackTrace();}
		
		ModelAddress modelAddress = this.modelService.addressCreate(street,number,idModel);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", modelAddress);
		_result.put("success", true);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelAddressCommand command){
		List<ModelAddress> modelAddress = null;
		
		Integer idModel = null;
		
		idModel = command.getIdModel();
		
		if (idModel!=null){
			Model model = this.modelService.retrieve(idModel);
			modelAddress = new ArrayList<ModelAddress>(model.getModeladdresses());
		}else{
			//modelAddress = this.modelService.addressRetrieve();
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", modelAddress);
		_result.put("total", modelAddress.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ModelAddressCommand command){
		
		Integer id = null;
		String street = null;
		Integer number = null;
		String apartment = null;
		
		try {id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		try {street = command.getData().getString("street");
		} catch (JSONException e) {e.printStackTrace();}
		try {number = command.getData().getInt("number");
		} catch (JSONException e) {e.printStackTrace();}
		try {apartment = command.getData().getString("apartment");
		} catch (JSONException e) {e.printStackTrace();}
		
		ModelAddress modelAddress = this.modelService.addressRetrieve(id);
		if (street!=null)
			modelAddress.setStreet(street);
		if (number!=null)
			modelAddress.setNumber(number);
		if (apartment!=null)
			modelAddress.setApartment(apartment);
		
		modelAddress = this.modelService.addressUpdate(modelAddress);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", modelAddress);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ModelAddressCommand command){

		Integer id = null;
		try {id = command.getData().getInt("id");}
		catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.modelService.addressDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
}
